/*
 * Filename WbcReportAnalysis.java 2022年08月31日
 * Copyright © Ehome Co.Ltd. All Rgiths Reserved.
 * @author wuyingchao
 */
package com.ehome.poct.core.service.analysis;

import cn.hutool.core.util.StrUtil;
import com.ehome.poct.core.api.constant.enums.ReportAnalysisType;
import com.ehome.poct.core.api.virtual.BldAnalysisService;
import com.ehome.poct.core.api.virtual.BldUniAnalysisService;
import com.ehome.poct.core.api.virtual.ConfAnalysisPermissionService;
import com.ehome.poct.core.api.vo.CheckResultVO;
import com.ehome.poct.core.api.vo.report.ReportAnalysisResultVO;
import com.ehome.poct.core.repository.report.CrpSaaConclusionRepository;
import com.ehome.poct.core.service.mapping.H5ReportMapping;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * WBC报告分析
 *
 * @author wyc
 * @since 2022/08/31 10:28
 **/
@Service
@RequiredArgsConstructor
public class WbcAnalysis {
    private final BldAnalysisService bldAnalysisService;
    private final ConfAnalysisPermissionService permissionService;
    private final BldUniAnalysisService bldUniAnalysisService;
    private final CrpSaaConclusionRepository crpSaaConclusionRepository;
    private final H5ReportMapping reportMapping;

    public ReportAnalysisResultVO analysis(List<CheckResultVO> results, String code, Long clinicId) {
        // 没有权限返回空
        if (!permissionService.hasPermission(ReportAnalysisType.WBC.getCode(), clinicId)) {
            return ReportAnalysisResultVO.createEmpty();
        }
        final var order = List.of(1L, 2L, 3L);
        final var abnormal = results.stream().filter(
                r -> order.contains(r.getOrder()) && r.getStatus() == 2).collect(Collectors.toList());
        final var analysisContent = reportMapping.to(bldAnalysisService.getWbcByCode(code));
        return new ReportAnalysisResultVO(abnormal, null, analysisContent);
    }

    public ReportAnalysisResultVO unionAnalysis(List<CheckResultVO> bldResults, List<CheckResultVO> jbyResults, BigDecimal crp, BigDecimal saa, String uniCode, Long clinicId) {
        // 没有权限返回空
        if (!permissionService.hasPermission(ReportAnalysisType.WBC.getCode(), clinicId)) {
            return ReportAnalysisResultVO.createEmpty();
        }
        // 三联测报告分析
        final var analysis = reportMapping.to(bldUniAnalysisService.getCrpSaaUniByCode(uniCode));
        // crp+saa病症结论
        if (analysis.getConclusion() == null || StrUtil.isEmpty(analysis.getConclusion().getName())) {
            reportMapping.to(analysis, crpSaaConclusionRepository.getByValue(crp, saa, uniCode.substring(0,7)));
        }
        // 血常规异常检测数据
        final var wbcOrder = List.of(1L, 2L, 3L, 4L, 5L);
        final var abnormal = bldResults.stream().filter(
                r -> wbcOrder.contains(r.getOrder()) && r.getStatus() == 2).collect(Collectors.toList());
        // 金标仪异常数据
        abnormal.addAll(jbyResults.stream().filter(r -> r.getStatus() == 2).collect(Collectors.toList()));
        return new ReportAnalysisResultVO(abnormal, null, analysis);
    }
}